package data

import (
	"fmt"
	"yunj/pkg/global"
	"yunj/pkg/yunj/app/model"
	"yunj/pkg/yunj/core/db"
)

// 初始化通知数据库
func InitNoticeDbData() (err error) {
	noticeDb := db.Model(&model.AdminNotice{})
	tableName := noticeDb.GetTableName()
	// 创建数据表
	tableSql := "CREATE TABLE IF NOT EXISTS `" + tableName + "` (" +
		"`id` bigint unsigned NOT NULL," +
		"`type` tinyint unsigned NOT NULL DEFAULT '0' COMMENT '类型。11 任务通知'," +
		"`member_id` int unsigned NOT NULL DEFAULT '0' COMMENT '通知成员id'," +
		"`desc` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '描述'," +
		"`data_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '数据id'," +
		"`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP," +
		"`read_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '阅读时间'," +
		"PRIMARY KEY (`id`) USING BTREE," +
		"KEY `idx_member_id` (`member_id`) USING BTREE," +
		"KEY `idx_created_at` (`created_at`) USING BTREE," +
		"KEY `idx_read_at` (`read_at`) USING BTREE" +
		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='后台通知表';"
	err = global.MySQL.Exec(tableSql).Error
	if err != nil {
		err = fmt.Errorf("创建数据表%s失败！%v", tableName, err)
		return
	}
	return
}
